const {createOrUpdate,findCarts,updateCarts,removeCarts,selectAllCarts}=require('../service/cart.server')
class CartController{
    async addCart(ctx){
        //解析参数，操作数据库，返回数据
        const user_id=ctx.state.user.id
        const goods_id=ctx.request.body.goods_id
            //??????????????????????????????????
           const res= await createOrUpdate(user_id,goods_id)
          ctx.body={
            code:0,
            message:"加入购物车成功",
              result:res
        }

    }
    async findAll(ctx){
        const user_id=ctx.state.user.id
      const res= await  findCarts(user_id)

          ctx.body={
              message:"获取购物车列表成功",
              result:res
          }
    }
    async updateC(ctx){

        const user_id=ctx.state.user.id
        const {id}=ctx.request.params
        const {number,selected}=ctx.request.body

        if(number===undefined && selected===undefined){
            return ctx.body={
                message: '数量跟选择不能都为空'
            }
        }
        console.log(number)
        console.log(selected)
        console.log(id)

        const res=await updateCarts({id,number,selected,user_id})
        if(res[0]==1){
            ctx.body={
                code:200,
                message:"修改购物车成功"
            }
        }else {
            ctx.body={
                message:"修改购物车失败"
            }
        }


    }
    async remove(ctx,next){
        const user_id=ctx.state.user.id
        console.log(user_id)
        const {ids}=ctx.request.body
        const res=await removeCarts(ids,user_id)
        console.log(res)
        ctx.body={
            message:"删除购物车成功"
        }
    }
    async SelectAll(ctx){
        const user_id=ctx.state.user.id
        const res=await selectAllCarts(user_id)
        ctx.body={
            message:"全选"
        }

    }
}
module.exports=new CartController()